package gui.util.event;

import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JCheckBox;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 * Mouse listener for the checkboxes in the list. This is needed to implement
 * the checking of checkboxes when user clicks on an item in the list.
 *  
 * @author Jakub Závěrka
 */
public  class CheckboxMouseListener extends MouseAdapter {
	/**
	 * This method will cause the selected checkbox go the opposite state. 
	 * (Selected if unselected, or unselected if selected).  
	 */
	@Override 
	public void mouseClicked(MouseEvent e){
		 //System.out.println(e.getComponent());
		 
		 int x = e.getX();
		 int y = e.getY();
		 
		 Component c = e.getComponent();
		 JTree tree = (JTree)c;

		 TreePath path = tree.getPathForLocation(x, y);
		 if (path == null) {
			 // click outside any node
			 return;
		 }
		 DefaultMutableTreeNode node = 
			 (DefaultMutableTreeNode)path.getLastPathComponent();
		 JCheckBox cb = (JCheckBox)node.getUserObject();
		 cb.setSelected(!cb.isSelected());
		 tree.repaint();
		 
		
//
//		JCheckBox item = (JCheckBox)model.getElementAt(selectedIndex);
//		item.setSelected(!item.isSelected());
//		repaint();
	}
}
